<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发票粘贴单</title>
    <style>
        body {
            font-family: 'SimSun', '宋体', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #fff;
            display: flex;
            justify-content: center;
        }
        .container {
            background-color: white;
            padding: 0;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            position: relative;
            margin: 0;
            height: 210mm;
            border-radius: 0;
        }
        .invoice-sheet {
            position: relative;
            margin: 0;
            padding: 15px;
            transition: all 0.3s ease;
            width: 100%;
            height: 100%;
            box-sizing: border-box;
        }
        /* 背景颜色选项 */
        .invoice-sheet.bg-white {
            background-color: white;
        }
        .invoice-sheet.bg-blue {
            background-color: #e6f4ff;
        }
        .invoice-sheet.bg-green {
            background-color: #e8f5e9;
        }
        .invoice-sheet.bg-yellow {
            background-color: #fff9e6;
        }
        .invoice-sheet.bg-gray {
            background-color: #f5f5f5;
        }
        .sheet-header {
            margin-bottom: 15px;
            text-align: center;
        }
        .sheet-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 8px;
            color: #333;
        }
        .reason-input {
            border-bottom: 1px solid #333;
            flex-grow: 1;
            min-width: 200px;
            padding: 3px;
        }
        .paste-area {
            display: flex;
            margin-bottom: 15px;
            width: 100%;
            height: calc(100% - 160px); 
        }
        .binding-area {
            width: 200mm;
            writing-mode: vertical-rl;
            text-align: center;
            font-size: 20px;
            color: #ccc;
            border: 1px dashed #333;
            padding-right: 8px;
            margin-right: 10px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .main-paste-area {
            flex-grow: 1;
            width: calc(100% - 180mm); 
        }
        .invoice-row {
            display: flex;
            margin-bottom: 8px;
            align-items: center;
            justify-content: flex-end;
        }
        .invoice-formula {
            display: flex;
            align-items: center;
            width: 100%;
            justify-content: flex-end;
        }
        .invoice-formula-part {
            border-bottom: 1px solid #333;
            padding: 0 5px;
            text-align: center;
            min-width: 60px;
            height: 24px;
            line-height: 24px;
        }
        .invoice-formula-operator {
            margin: 0 5px;
        }
        .total-info {
            margin-top: 15px;
            padding-top: 8px;
            width: 100%;
        }
        .total-row {
            display: flex;
            margin-bottom: 5px;
        }
        .total-label {
            width: 80px;
        }
        .total-value {
            padding: 0 5px;
        }
        .amount-in-words {
            margin-top: 8px;
            font-weight: bold;
            font-size: 14px;
        }
        .signature-area {
            margin-top: 20px;
            display: flex;
            justify-content: space-between;
            width: 100%;
            position: absolute;
            bottom: 15px;
            left: 0;
            padding: 0 15px;
            box-sizing: border-box;
        }
        .signature {
            width: 25%;
        }
        .signature-label {
            margin-bottom: 5px;
        }
        .signature-line {
            border-bottom: 1px solid #333;
            height: 24px;
        }
        .controls-container {
            position: fixed;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            z-index: 100;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        .print-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .print-btn:hover {
            background: linear-gradient(135deg, #5a6fc5 0%, #6a3e91 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
        }
        .print-btn:active {
            transform: translateY(0);
        }
        .editable {
            border-bottom: 1px dotted #666;
            display: inline-block;
            padding: 2px 5px;
            text-align: left;
            transition: all 0.2s ease;
        }
        .editable:focus {
            outline: none;
            border-bottom: 2px solid #2196F3;
            background-color: #f9f9f9;
        }
        .add-row-btn {
            background: linear-gradient(135deg, #5a6fc5 0%, #6a3e91 100%);
            color: white;
            border: none;
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 8px;
            font-size: 14px;
            transition: all 0.2s ease;
        }
        .add-row-btn:hover {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        /* 背景选择器样式 */
        .bg-selector {
            background: white;
            padding: 12px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }
        .bg-selector h3 {
            margin-top: 0;
            margin-bottom: 8px;
            font-size: 14px;
            color: #333;
            border-bottom: 1px solid #eee;
            padding-bottom: 5px;
        }
        .bg-option {
            display: block;
            margin: 4px 0;
            padding: 6px 8px;
            border-radius: 3px;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            font-size: 14px;
        }
        .bg-option:hover {
            background: #f0f0f0;
        }
        .bg-option.active {
            background: #e0e0ff;
            font-weight: bold;
            color: #333;
        }
        .bg-option .color-preview {
            width: 14px;
            height: 14px;
            border-radius: 50%;
            margin-right: 6px;
            display: inline-block;
            border: 1px solid #ddd;
        }
        .paste-label{
            text-align: center;
            margin-top: 15px;
            display: flex;
            letter-spacing: 0.1em;
        }
        .invoiceRows{
            margin-top: 20px;
        }
        /* 打印样式 */
        @media print {
            @page {
                size: A4 landscape;
                margin: 0;
            }
            body {
                background-color: white;
                padding: 0;
                margin: 0;
                width: 297mm;
                height: 210mm;
            }
            .container {
                max-width: none;
                box-shadow: none;
                padding: 0;
                margin: 0;
                width: 100%;
                height: 100%;
            }
            .invoice-sheet {
                margin: 0;
                padding: 15px;
                width: 100%;
                height: 100%;
                box-sizing: border-box;
            }
            .controls-container {
                display: none !important;
            }
            .editable {
                border-bottom: 1px dotted #000;
            }
            /* 新增规则：隐藏添加行按钮 */
            .add-row-btn {
                display: none !important;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="invoiceSheet" class="invoice-sheet bg-white">
            <div class="sheet-header">
                <div class="sheet-title">发票粘贴单</div>
            </div>
            
            <div class="paste-area">
                <div class="binding-area">发票粘贴区</div>
                <div class="main-paste-area">
                    <div class="paste-label" style="font-weight: bold;">面额*张数=金额</div>
                    
                    <div id="invoiceRows">
                        <div class="invoice-row">
                            <div class="invoice-formula">
                                <div class="invoice-formula-part editable amount-input" contenteditable="true" style="width:40px;text-align:center;">0</div>
                                （元）
                                <div class="invoice-formula-operator">×</div>
                                <div class="invoice-formula-part editable count-input" contenteditable="true" style="width:40px;text-align:center;">1</div>
                                （张）
                                <div class="invoice-formula-operator">=</div>
                                <div class="invoice-formula-part editable total-input" contenteditable="true" style="width:40px;text-align:center;">0</div>
                                （元）
                            </div>
                        </div>
                        <!-- 更多行将通过JavaScript添加 -->
                    </div>
                    
                    <button id="addRowBtn" class="add-row-btn">添加一行</button>
                    
                    <div class="total-info">
                        <div class="total-row">
                            <div class="total-label">票据总计</div>
                            <div class="total-value editable" contenteditable="true" id="totalCount" style="width:40px;text-align:center;"></div>
                            <span style="margin-left: 5px;">张</span>
                        </div>
                        <div class="total-row">
                            <div class="total-label">金额总计</div>
                            <div class="total-value">¥ <span class="editable" contenteditable="true" id="totalAmount"></span> 元</div>
                        </div>
                        <div class="amount-in-words">金额大写：<span id="amountInWords"></span></div>
                    </div>

                    <div class="paste-label">
                        <div class="reason-input editable" contenteditable="true"></div>
                    </div>
                </div>
            </div>
            
            <div class="signature-area">
                <div class="signature">
                    <div class="signature-label" contenteditable="true">经办人：</div>
                    <div class="signature-line"></div>
                </div>
                <div class="signature">
                    <div class="signature-label" contenteditable="true">复核：</div>
                    <div class="signature-line"></div>
                </div>
                <div class="signature">
                    <div class="signature-label" contenteditable="true">财务：</div>
                    <div class="signature-line"></div>
                </div>
            </div>
        </div>
        
        <!-- 控制面板 -->
        <div class="controls-container">
            <!-- 背景选择器 -->
            <div class="bg-selector">
                <h3>选择背景</h3>
                <div class="bg-option active" data-bg="bg-white">
                    <span class="color-preview" style="background-color: white;"></span>
                    纯白色
                </div>
                <div class="bg-option" data-bg="bg-blue">
                    <span class="color-preview" style="background-color: #e6f4ff;"></span>
                    淡蓝色
                </div>
                <div class="bg-option" data-bg="bg-green">
                    <span class="color-preview" style="background-color: #e8f5e9;"></span>
                    淡绿色
                </div>
                <div class="bg-option" data-bg="bg-yellow">
                    <span class="color-preview" style="background-color: #fff9e6;"></span>
                    淡黄色
                </div>
                <div class="bg-option" data-bg="bg-gray">
                    <span class="color-preview" style="background-color: #f5f5f5;"></span>
                    淡灰色
                </div>
            </div>
            
            <!-- 打印按钮 -->
            <button type="button" id="printSheet" class="print-btn">
                打印粘贴单
            </button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 打印粘贴单
            document.getElementById('printSheet').addEventListener('click', function() {
                window.print();
            });
            
            // 添加行
            document.getElementById('addRowBtn').addEventListener('click', function() {
                const invoiceRows = document.getElementById('invoiceRows');
                const newRow = document.createElement('div');
                newRow.className = 'invoice-row';
                newRow.innerHTML = `
                    <div class="invoice-formula">
                        <div class="invoice-formula-part editable amount-input" contenteditable="true" style="width:40px;text-align:center;">0</div>
                        （元）
                        <div class="invoice-formula-operator">×</div>
                        <div class="invoice-formula-part editable count-input" contenteditable="true" style="width:40px;text-align:center;">1</div>
                        （张）
                        <div class="invoice-formula-operator">=</div>
                        <div class="invoice-formula-part editable total-input" contenteditable="true" style="width:40px;text-align:center;">0</div>
                        （元）
                    </div>
                `;
                invoiceRows.appendChild(newRow);
                
                // 为新行添加事件监听
                setupRowEventListeners(newRow);
            });
            
            // 设置行的计算逻辑
            function setupRowEventListeners(row) {
                const amountInput = row.querySelector('.amount-input');
                const countInput = row.querySelector('.count-input');
                const totalInput = row.querySelector('.total-input');
                
                function calculateTotal() {
                    const amount = parseFloat(amountInput.textContent) || 0;
                    const count = parseInt(countInput.textContent) || 0;
                    const total = amount * count;
                    totalInput.textContent = total.toFixed(2);
                    updateTotals();
                }
                
                amountInput.addEventListener('input', calculateTotal);
                countInput.addEventListener('input', calculateTotal);
            }
            
            // 初始化现有行的监听
            document.querySelectorAll('.invoice-row').forEach(row => {
                setupRowEventListeners(row);
            });
            
            // 更新总计
            function updateTotals() {
                let totalAmount = 0;
                let totalCount = 0;
                
                document.querySelectorAll('.invoice-row').forEach(row => {
                    const amount = parseFloat(row.querySelector('.amount-input').textContent) || 0;
                    const count = parseInt(row.querySelector('.count-input').textContent) || 0;
                    const total = parseFloat(row.querySelector('.total-input').textContent) || 0;
                    
                    if (amount && count) {
                        totalAmount += total;
                        totalCount += count;
                    }
                });
                
                document.getElementById('totalAmount').textContent = totalAmount.toFixed(2);
                document.getElementById('totalCount').textContent = totalCount;
                document.getElementById('amountInWords').textContent = numberToChinese(totalAmount);
            }
            
            // 数字转中文大写
            function numberToChinese(num) {
                if (isNaN(num) || num === 0) return '零元整';
                
                const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
                const units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '兆'];
                
                // 处理整数部分
                let str = '';
                const numStr = Math.floor(num).toString();
                const len = numStr.length;
                
                for (let i = 0; i < len; i++) {
                    const digit = parseInt(numStr[i]);
                    const pos = len - i - 1;
                    
                    if (digit !== 0) {
                        str += digits[digit] + units[pos];
                    } else {
                        // 当前位是0，判断是否需要补零
                        if (pos === 4 || pos === 8) {
                            // 万、亿位的0需要补单位
                            str += units[pos];
                        }
                        
                        // 下一位非零则补零
                        if (i < len - 1 && parseInt(numStr[i + 1]) !== 0) {
                            str += digits[0];
                        }
                    }
                }
                
                str += '元';
                
                // 处理小数部分（角分）
                const decimal = Math.round((num - Math.floor(num)) * 100);
                const jiao = Math.floor(decimal / 10);
                const fen = decimal % 10;
                
                if (jiao > 0) {
                    str += digits[jiao] + '角';
                }
                
                if (fen > 0) {
                    str += digits[fen] + '分';
                } else if (jiao === 0) {
                    str += '整';
                }
                
                return str;
            }
            
            // 背景选择功能
            const bgOptions = document.querySelectorAll('.bg-option');
            const invoiceSheet = document.getElementById('invoiceSheet');
            
            bgOptions.forEach(option => {
                option.addEventListener('click', function() {
                    // 移除所有active类
                    bgOptions.forEach(opt => opt.classList.remove('active'));
                    // 添加当前active类
                    this.classList.add('active');
                    // 更新粘贴单背景
                    const bgClass = this.getAttribute('data-bg');
                    // 移除所有背景类
                    invoiceSheet.className = 'invoice-sheet';
                    // 添加选中的背景类
                    invoiceSheet.classList.add(bgClass);
                });
            });
        });
    </script>
</body>
</html>